﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;

namespace IpQuery
{
    /// <summary>
    /// 主机帮助类，
    /// 指定host或者url，返回服务器的ip地址
    /// </summary>
    public class DomainHelper
    {
        /// <summary>
        /// 使用Ping类，返回获取指定域名的ip
        /// </summary>
        /// <param name="domain">域名</param>
        /// <returns></returns>
        public static string GetIp_Ping(string domain)
        {
            Ping p1 = new Ping();
            PingReply replay = p1.Send(domain);
            if (replay.Status == IPStatus.Success)
            {
                return replay.Address.ToString();
            }
            else
            {
                throw new Exception(string.Format("获取域名'{0}'的ip地址失败", domain));
            }
        }
        /// <summary>
        /// 指定域名或url获取ip地址
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string GetIp(string url)
        {
            if (RegexHelper.IsDomain(url))
                return GetIp_Ping(url);
            else if (RegexHelper.IsURL(url))
            {
                Uri uri = new Uri(url);
                if (uri.HostNameType == UriHostNameType.Dns)
                {
                    string domain = uri.Host;
                    return GetIp_Ping(domain);
                }
            }
            throw new Exception("解析url内容失败");
        }
        /// <summary>
        /// 指定域名或url获取ip地址
        /// </summary>
        /// <param name="url"></param>
        /// <param name="domain">当前域名</param>
        /// <returns></returns>
        public static string GetIp(string url, out string domain)
        {
            if (RegexHelper.IsDomain(url))
            {
                domain = url;
                return GetIp_Ping(url);
            }
            else if (RegexHelper.IsURL(url))
            {
                Uri uri = new Uri(url);
                if (uri.HostNameType == UriHostNameType.Dns)
                {
                    domain = uri.Host;
                    return GetIp_Ping(domain);
                }
            }
            throw new Exception("解析url内容失败");
        }

        #region Ip地址和数字之间的转换
        /// <summary>
        /// 将ip地址，转换成数字
        /// </summary>
        /// <param name="ip">ip地址</param>
        /// <returns></returns>
        public static long IpToInt(string ip)
        {
            char[] separator = new char[] { '.' };
            string[] items = ip.Split(separator);
            return long.Parse(items[0]) << 24
                    | long.Parse(items[1]) << 16
                    | long.Parse(items[2]) << 8
                    | long.Parse(items[3]);
        }
       /// <summary>
       /// 将数字转换成ip地址
       /// </summary>
       /// <param name="ipInt">数字ip</param>
       /// <returns></returns>
        public static string IntToIp(long ipInt)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append((ipInt >> 24) & 0xFF).Append(".");
            sb.Append((ipInt >> 16) & 0xFF).Append(".");
            sb.Append((ipInt >> 8) & 0xFF).Append(".");
            sb.Append(ipInt & 0xFF);
            return sb.ToString();
        }
        #endregion
    }
}
